// pages/home.js
import store from './../../utils/store'
var WXAPI = require('./../../wxApi/main')
//获取应用实例
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
    data: {
      show: false, //控制下拉列表的显示隐藏，false隐藏、true显示
      selectData: ['今天','本周','本月'], //下拉列表的数据
      index: 0, //选择的下拉列表下标,
      modalName: store.getItem('modalName'),
      elements: [{
          title: '订单管理',
          name: 'order',
          color: 'cyan',
          icon: './../../images/gl.png'
        },{
          title: '商品管理',
          name: 'goodsManagement',
          color: 'cyan',
          icon: './../../images/sp.png'
        },{
          title: '活动策划',
          name: 'active',
          color: 'cyan',
          icon: './../../images/hd.png'
        },
        // {
        //   title: '店铺注册',
        //   name: 'registerShop',
        //   color: 'cyan',
        //   icon: './../../images/zc.png'
        // },
        // {
        //   title: '销量统计',
        //   name: 'layout',
        //   color: 'cyan',
        //   icon: './../../images/tj.png'
        // },
        {
          title: '店铺管理',
          name: 'switchShop',
          color: 'cyan',
          icon: './../../images/qh.png'
        },
      ],
      loginInfo:{
        "password": "",
        "requestMobileDto": {
          "encryptedData": "",
          "iv": ""
        },
        "smallRoutineLoginDto": {
          "code": "",
          "encryptedData": "",
          "iv": "",
          "rawData": "",
          "signature": ""
        },
        "username": ""
      }
    },
    // 点击下拉显示框
    selectTap(){
      this.setData({
        show: !this.data.show
      });
    },
    // 点击下拉列表
    optionTap(e){
      let Index = e.currentTarget.dataset.index;//获取点击的下拉列表的下标
      console.log(Index)
      this.initData(Index)
      this.setData({
        index: Index,
        show: !this.data.show
      });
    },

    initData:function(type){
      let _this=this
      WXAPI.index({
        type: type
      }).then(res => {
        let data = res.data
        let orderTotal = data.orderTotal
        let yesterDayOrderTotal = data.yesterDayOrderTotal
        let subOrderTotal = orderTotal - yesterDayOrderTotal
        if (subOrderTotal > 0) {
          data.orderTag = '+' + subOrderTotal + ' ↑'
        } else if (subOrderTotal == 0) {
          data.orderTag = subOrderTotal
        } else {
          data.orderTag = subOrderTotal + ' ↓'
        }
        let totalPrice = data.totalPrice
        let yesterDayMoney = data.yesterDayMoney
        let subPrice = totalPrice - yesterDayMoney
        if (subPrice > 0) {
          data.priceTag = '+' + subPrice + ' ↑'
        } else if (subPrice == 0) {
          data.priceTag = subPrice
        } else {
          data.priceTag = subPrice + ' ↓'
        }
        _this.setData({
          data: data,
          first:1
        })
        console.log(data)
      }).catch(err => {
        console.log(err)
      })
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 判断是否登录
    if(store.getItem("userInfo")){
      this.setData({
        modalName: null
      })
      store.setItem("modalName",null)
      this.initData(0)
    }else{
      this.setData({
        modalName: 'login'
      })
      store.setItem("modalName","login")
      // wx.navigateTo({
      //   url: '/pages/index/index',
      // })
    }
   
  },
  // 模态框控制
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  bindGologin(){
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log("onshow()")
    if(this.data.first){
      this.initData(0)
    }
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})